package com.stars.easyms.mq.initializer;

import com.stars.easyms.datasource.mybatis.EasyMsMapperScannerConfigurer;
import com.stars.easyms.mq.annotation.MQRepository;
import com.stars.easyms.mq.util.EasyMsMQPackageUtil;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * <p>className: MQRepositoryInitializer</p>
 * <p>description: MQ数据持久化初始化类</p>
 *
 * @author guoguifang
 * @date 2019-10-29 18:08
 * @since 1.3.3
 */
public final class MQRepositoryInitializer {

    private ConfigurableApplicationContext applicationContext;

    /**
     * 扫描Repository Package下所有DAO接口
     */
    public void init() {
        EasyMsMapperScannerConfigurer mapperScannerConfigurer = new EasyMsMapperScannerConfigurer(applicationContext);
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("easyMsMQSqlSessionFactory");
        mapperScannerConfigurer.setBasePackage(EasyMsMQPackageUtil.getRepositoryPackageName());
        mapperScannerConfigurer.setAnnotationClass(MQRepository.class);
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        defaultListableBeanFactory.registerSingleton(EasyMsMQPackageUtil.getRootPackageName() + ".mapperScannerConfigurer", mapperScannerConfigurer);
        mapperScannerConfigurer.postProcessBeanDefinitionRegistry(defaultListableBeanFactory);
    }

    public MQRepositoryInitializer(ConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}